package com.villaday.idf.activity;

import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.custom.ChooseAddrDialog;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model2.NewAndEdit;
import com.villaday.idf.model2.ShippingAddresses;
import com.villaday.idf.model2.ShippingAddressesEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *新建地址 编辑地址
 * */
public class NewAndEditAddrActivity extends BaseActivity implements OkHttpUtil.OnDataListener, BaseDialog.OnItemClickListener {


    @BindView(R.id.toolbar_iconNext)
    ImageButton toolbarIconNext;
    @BindView(R.id.toolbar_icon)
    Toolbar toolbarIcon;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_addr)
    EditText etAddr;
    @BindView(R.id.togglebutton)
    ToggleButton togglebutton;

    @BindView(R.id.toolbar_iconBack)
    ImageButton toolbar_iconBack;


    private String code;
    private String id;


    private Gson gson = new GsonBuilder().create();
    private int type;
    private LoadingDialog loadingDialog;
    private ShippingAddressesEntity.BodyEntity entity;


    @Override
    protected int getViewResId() {
        return R.layout.activity_new_and_edit_addr;


    }

    @Override
    protected void init() {
        toolbar_iconBack.setFocusable(true);
        toolbar_iconBack.setFocusableInTouchMode(true);
        type = getIntent().getIntExtra("type", 0);
        boolean isSelect = getIntent().getBooleanExtra("isSelect", false);
        togglebutton.setChecked(isSelect);
        if (type == 0) {//新建地址
            ToolBarUtil.initToolBar(toolbarIcon, "新建地址", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            if (type == 1) {//修改
                ToolBarUtil.initToolBar(toolbarIcon, "编辑地址", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                }, R.drawable.public_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (entity != null) {
                            loadingDialog = LoadingDialog.newInstance("操作中...");
                            loadingDialog.show(getFragmentManager());
                            ShippingAddresses shippingAddresses = new ShippingAddresses(2 + "", entity.getId(), entity.getCode(), entity.getAddress(), entity.getLinkman(), entity.getMobile(), entity.isPreferred());//string,操作，0 新增/修改  2 删除",
                            Map<String, Object> map = new HashMap<>();

                            OkHttpUtil.postJson(NewAndEditAddrActivity.this, Constant.URL.shippingAddresses, shippingAddresses, map, NewAndEditAddrActivity.this);
                        }
                    }
                });

                entity = getIntent().getParcelableExtra("entity");
                id = entity.getId();
                code = entity.getCode();
                tvCity.setText(entity.getRegion());
                etName.setText(entity.getLinkman());
                etPhone.setText(entity.getMobile());
                tvCity.setText(entity.getRegion());
                etAddr.setText(entity.getAddress());
                togglebutton.setChecked(entity.isPreferred());
            }
        }
    }

    @OnClick({R.id.tv_city, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_city:
                ChooseAddrDialog chooseAddrDialog = ChooseAddrDialog.newInstance();
                chooseAddrDialog.show(getFragmentManager());
                chooseAddrDialog.setOnItemClickListener(this);
                break;
            case R.id.tv_save:

                if (TextUtils.isEmpty(etName.getText())) {
                    ToastUtil.initToast(this, "请填写联系人姓名");
                } else if (TextUtils.isEmpty(etPhone.getText())) {
                    ToastUtil.initToast(this, "请填写联系人手机号");
                } else if (TextUtils.isEmpty(code)) {
                    ToastUtil.initToast(this, "请选择区域");
                } else if (TextUtils.isEmpty(etAddr.getText())) {
                    ToastUtil.initToast(this, "请填写详细地址");
                } else {
                    loadingDialog = LoadingDialog.newInstance("操作中...");
                    loadingDialog.show(getFragmentManager());
                    ShippingAddresses shippingAddresses = new ShippingAddresses(0 + "", id, code, etAddr.getText().toString(), etName.getText().toString(), etPhone.getText().toString(), togglebutton.isChecked());//string,操作，0 新增/修改  2 删除",
                    Map<String, Object> map = new HashMap<>();

                    OkHttpUtil.postJson(this, Constant.URL.shippingAddresses, shippingAddresses, map, this);
                }


                break;
        }

    }


    @Override
    public void onResponse(String url, String json) {
        dismissLoading();
        if (!TextUtils.isEmpty(json)) {
            LogeUtil.e("shippingAddresses", json);
            NewAndEdit newAndEdit = gson.fromJson(json, NewAndEdit.class);
            if ("200".equals(newAndEdit.getCode())) {

                ToastUtil.initToast(this, "操作成功");

                setResult(RESULT_OK);
                finish();

            } else {//失败
                ToastUtil.initToast(this, newAndEdit.getMsg());

            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    @Override
    public void onItemClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm: {
               /* v.setTag(R.id.tag_first,tvTitle.getText().toString());
                v.setTag(R.id.tag_second,code);*/
                if (!TextUtils.isEmpty((String) v.getTag(R.id.tag_second))) {
                    tvCity.setText((String) v.getTag(R.id.tag_first));
                    code = (String) v.getTag(R.id.tag_second);
                }

            }
            break;
        }
    }

    private void dismissLoading() {

        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }

    }
}
